const app = getApp()
Page({
  data: {
    loading: true,
    onLike:false,
    show_follow:true,
    keyword:'',
    page: 0,
    info: [],
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    this.setData({
      keyword:options.keyword,
      uid:app.globalData.loginuser.uid
    });
    this.infoApi();
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
    this.setData({
      page: 0,
      loading: true,
      info: []
    });
    this.infoApi();
  },
  //上拉加载
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.infoApi();
  },
  //点击新闻
  infoApi: function () {
    if (this.data.loading) {
      var param = {
        cate_id:0,
        state:0,
        keyword:this.data.keyword,
        page: this.data.page + 1,
      }
      app.api().Get('api/v1/ais/info/index',param,(result)=>{
        if (result.code == 200) {
          var info = this.data.info;
          for (let i in result.data) {
            info.push(result.data[i]);
          }
          this.setData({
            info: info,
          });
          this.data.page = param.page
        }
        this.setData({
          loading: false,
        });
      })
    }
  },
  //关注与取消
  onFollow:function (e) {
    var key = e.currentTarget.dataset.key;
    var info = this.data.info;
    if (info[key].is_follow == 0){
      app.api().Post("api/v1/ais/infosend/follow",{uid:info[key].user_id,state:1},(rel) => {
        info[key].is_follow = 1;
        this.setData({
          info:info,
        })
      })
    }
  },
  //点赞
  onLike:function(e){
    let key = e.currentTarget.dataset.key,id = e.currentTarget.dataset.id;
    var info = this.data.info;
    if (info[key].is_like == 0){
      app.api().Post("api/v1/ais/infosend/like",{id:id},(rel) => {
        info[key].is_like = 1;
        this.setData({
          info:info,
          onLike:true
        })
        setTimeout(()=>{
          this.setData({
            onLike:false
          })
        },600)
      })
    }
  },
  //搜索
  onSearch: function (e) {
    this.setData({
      page: 0,
      loading: true,
      info: [],
      keyword:e.detail,
    })
    this.infoApi();
  },
  //拨打电话
  callPhone:function (e) {
    app.callPhone(e);
  },
  //图片预览
  previewImage: function (e) {
    var key = e.target.dataset.key;
    wx.previewImage({
      current: this.data.info[key].img,
      urls: this.data.info[key].images
    })
  },
  //分享按钮
  onShareAppMessage: function (e) {
    return {
      title: app.globalData.appConfig.shore_text,
      imageUrl:app.globalData.appConfig.shore_img,
      path: '/packageInfo/pages/info/index?ucode=' + app.globalData.loginuser.ucode
    }
  },
  //滚动
  onPageScroll(res){
    let scrollTop = res.scrollTop;
    this.setData({
      showbar: 5 <= scrollTop ?true :false
    })
  }
})